<template>
	<view class="myuploadimg">
		<view class="upload_box" @click="chooseImage" :style="{ width: `${up_width}rpx`, height: `${up_width}rpx` }">
			<view class="upload_view">
				<image :src="upurl ? upurl : url" class="upload_pic" mode="aspectFill" @error="imgError"></image>
				<view class="upload_press" v-if="upload_press_show == 1">{{ upload_press }}%</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	name: 'myuploadimg',
	props: {
		key_name: {
			type: [String],
		},
		url: {
			type: [String],
			default() {
				return '';
			},
		},
		upload_type: {
			type: [String],
			default() {
				return '';
			},
		},
		up_width: {
			type: [String, Number],
			default() {
				return '90';
			},
		},
	},
	watch: {},
	data() {
		return {
			upload_press_show: 0,
			upload_press: 0,
			upurl: '',
			choose_list: [],
		};
	},
	mounted() {
		// console.log(this.base64_en('http://tmp/BmjWzO9RGpNQefcdacdb58715cf5512f8bf3a21c985a.png'))
	},
	methods: {
		imgError(e) {
			// debugger
		},
		chooseImage() {
			let _this = this;
			//图片上传
			uni.chooseImage({
				mediaType: ['image'],
				count: 1, //默认9
				sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				success: function (res) {
					debugger;
					// for (let item of res.tempFiles) {
					// 	_this.choose_list.push(item.path)
					// }
					_this.choose_list = res.tempFilePaths;
					_this.upload_now(res.tempFilePaths);

					// _this.upload_F(res.tempFilePaths, 0);
					console.log(_this.choose_list, '_this.choose_list');
					// console.log(res, 'res')
				},
				fail(err) {
					console.log(err);
				},
			});
		},

		upload_now(list) {
			let _this = this;
			for (let i = 0; i < list.length; i++) {
				_this.upload_ele(list[i]);
			}
		},
		async upload_ele(url, sc) {
			let _this = this;
			let str = await _this.base64_en(url);
			let type = this.upload_type;
			// return
			_this.com_getdata({
				url: '/base/upload',
				data_handle_on: false,
				data: {
					type,
					str,
				},
				data_fun: function (v) {
					if (v.state == 1) {
						// _this.com_msg(v.msg);
						_this.upurl = v.data;
						_this.$emit('upload_end', {
							value: v.data,
							key: _this.key_name,
						});
					} else {
						_this.com_msg(v.msg || '网络请求错误');
					}
				},
			});
		},

		formTrigger(e, item) {
			console.log(e, 'ee');
			let _this = this;
			if (!_this.formupdate) return;
			_this.formupdate = false;

			let { state } = item;
			if (state == 'edit') {
				if (_this.objJudge(item.regular)) {
					if (!_this.page_check(e.detail.value, item.regular)) return;
				} else {
					if (!e.detail.value) {
						return _this.com_msg(item.placeholder);
					}
				}
			}
			let o = {
				value: e.detail.value,
				key: item.key,
			};
			_this.$emit('formChange', o);
			_this.$nextTick(() => {
				_this.formupdate = true;
			});
		},
	},
};
</script>

<style lang="scss">
.upload_view {
	width: 100%;
	height: 100%;

	.upload_pic {
		width: 100%;
		height: 100%;
		border-radius: 50%;
	}
}
</style>
